package com.cgx.webmagic;

import com.cgx.dao.CategoryDao;
import com.cgx.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chenguanxin
 * @date 2020-07-16 21:47
 */
@Component
public class MyPageProcessor implements PageProcessor {

    @Autowired
    private CategoryDao categoryDao;



    @Override
    public void process(Page page) {

        for (int i = 1; i < 100; i++) {
            // /html/body/div[5]/div[2]/div[1]
            String html = page.getHtml().xpath("/html/body/div[5]/div[2]/div["+ i +"]").toString();
            if (!StringUtils.hasText(html)) {
                break;
            }
            System.out.println(html);

            //  一级菜单
            String oneCategoryRegular = "<h2>(.*?)</h2> ";
            Pattern oneCategoryPattern = Pattern.compile(oneCategoryRegular);
            Matcher oneCategoryMatcher = oneCategoryPattern.matcher(html);
            Category oneCategory = new Category();
            while (oneCategoryMatcher.find()) {
                System.out.println("Found value: " + oneCategoryMatcher.group(0) );
                System.out.println("Found value oneCategory: " + oneCategoryMatcher.group(1) );
                oneCategory.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                oneCategory.setCategoryName(oneCategoryMatcher.group(1));
                oneCategory.setParentId("0");
                oneCategory.setParentIds("0," + oneCategory.getId());
                oneCategory.setType("1");
                oneCategory.setPriority(i);
                oneCategory.setDelFlag(0);
                oneCategory.setCreateBy("1");
                oneCategory.setCreateDate(new Date());
                oneCategory.setUpdateBy("1");
                oneCategory.setUpdateDate(new Date());
                categoryDao.save(oneCategory);
            }

            //  二级菜单
            String twoCategoryRegular = "<div class=\"title-box clearfix\">" +
                    "<div class=\"t-left fl clearfix\">" +
                    "(.*?)" +
                    "</div>" +
                    "<div class=\"t-right fl clearfix\">" +
                    "(.*?)" +
                    "</div>" +
                    "</div>";
            Pattern twoCategoryPattern = Pattern.compile(twoCategoryRegular.replaceAll(" ", ""));
            Matcher twoCategoryMatcher = twoCategoryPattern.matcher(html.replaceAll("\r", "").replaceAll("\n", "").replaceAll(" ", ""));
            int twoCategoryPriority = 0;
            while (twoCategoryMatcher.find()) {
                System.out.println("Found value0: " + twoCategoryMatcher.group(0) );
                System.out.println("Found value1: " + twoCategoryMatcher.group(1) );
                System.out.println("Found value2: " + twoCategoryMatcher.group(2) );
                String twoCategoryNameRegular = "<ahref=\"//list.suning.com(.*?)>(.*?)</a>";
                Pattern twoCategoryNamePattern = Pattern.compile(twoCategoryNameRegular);
                Matcher twoCategoryNameMatcher = twoCategoryNamePattern.matcher(twoCategoryMatcher.group(1));
                Category twoCategory = new Category();
                while(twoCategoryNameMatcher.find()) {
                    twoCategoryPriority ++;
                    System.out.println("Found value0: " + twoCategoryNameMatcher.group(0) );
                    System.out.println("Found value1: " + twoCategoryNameMatcher.group(1) );
                    System.out.println("Found value2 twoCategory: " + twoCategoryNameMatcher.group(2) );
                    twoCategory.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    twoCategory.setCategoryName(twoCategoryNameMatcher.group(2));
                    twoCategory.setParentId(oneCategory.getId());
                    twoCategory.setParentIds(oneCategory.getParentIds() + "," + twoCategory.getId());
                    twoCategory.setType("2");
                    twoCategory.setPriority(twoCategoryPriority);
                    twoCategory.setDelFlag(0);
                    twoCategory.setCreateBy("1");
                    twoCategory.setCreateDate(new Date());
                    twoCategory.setUpdateBy("1");
                    twoCategory.setUpdateDate(new Date());
                    categoryDao.save(twoCategory);
                }


                //  三级菜单
                String threeCategoryNameRegular = "<ahref=\"//list.suning.com/(.*?)>(.*?)</a>";
                Pattern threeCategoryNamePattern = Pattern.compile(threeCategoryNameRegular);
                Matcher threeCategoryNameMatcher = threeCategoryNamePattern.matcher(twoCategoryMatcher.group(2));
                Category threeCategory = new Category();
                int threeCategoryPriority = 0;
                while(threeCategoryNameMatcher.find()) {
                    threeCategoryPriority ++;
                    System.out.println("Found value0: " + threeCategoryNameMatcher.group(0) );
                    System.out.println("Found value1: " + threeCategoryNameMatcher.group(1) );
                    System.out.println("Found value2 threeCategory: " + threeCategoryNameMatcher.group(2) );
                    threeCategory.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    threeCategory.setCategoryName(threeCategoryNameMatcher.group(2));
                    threeCategory.setParentId(twoCategory.getId());
                    threeCategory.setParentIds(twoCategory.getParentIds() + "," + threeCategory.getId());
                    threeCategory.setType("3");
                    threeCategory.setPriority(threeCategoryPriority);
                    threeCategory.setDelFlag(0);
                    threeCategory.setCreateBy("1");
                    threeCategory.setCreateDate(new Date());
                    threeCategory.setUpdateBy("1");
                    threeCategory.setUpdateDate(new Date());
                    categoryDao.save(threeCategory);
                }
            }
        }
    }

    @Override
    public Site getSite() {
        return Site.me()
                .setSleepTime(100)// 设置爬取间隔时间
                .setTimeOut(10000)// 设置爬取超时时间
                .setRetryTimes(100);// 设置重试次数
    }

}
